package com.carhailing.client.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayExchangeConfig {
    public static String exchangeName = "order.ttl.exchange";
    public static String orderQ = "order.ttl.queue";
    public static String dlxQ = "order.dlx.queue";

    @Bean
    public DirectExchange delayedExchange(){
        return ExchangeBuilder.directExchange(exchangeName).build();
    }

    @Bean
    public Queue orderQueue(){
        // 指定该队列的过期时间和死信队列
        Map<String , Object> properties = new HashMap<>();
        properties.put("x-message-ttl" , 15000);
        properties.put("x-dead-letter-exchange" , exchangeName);
        properties.put("x-dead-letter-routing-key" , "dead-letter");
        return QueueBuilder.durable(orderQ).withArguments(properties).build();
    }

    @Bean
    public Queue dlxQueue(){
        return QueueBuilder.durable(dlxQ).build();
    }

    @Bean
    public Binding dlxBinding1(){
        return BindingBuilder.bind(dlxQueue()).to(this.delayedExchange()).with("dead-letter");
    }

    @Bean
    public Binding ttlBinding1(){
        return BindingBuilder.bind(dlxQueue()).to(this.delayedExchange()).with("order");
    }

}
